<?php defined('SYSPATH') or die('No direct script access.');

header('X-Frame-Options:SAMEORIGIN');

// -- Environment setup --------------------------------------------------------

// Load the core Kohana class

require SYSPATH . 'classes/kohana/core' . EXT;

if (is_file(APPPATH . 'classes/kohana' . EXT))
{
    // Application extends the core
    require APPPATH . 'classes/kohana' . EXT;
}
else
{
    // Load empty core extension
    require SYSPATH . 'classes/kohana' . EXT;
}

/**
 * Set the default time zone.
 *
 * @link http://kohanaframework.org/guide/using.configuration
 * @link http://www.php.net/manual/timezones
 */
//date_default_timezone_set('America/Chicago');
date_default_timezone_set('Asia/Shanghai');

/**
 * Set the default locale.
 *
 * @link http://kohanaframework.org/guide/using.configuration
 * @link http://www.php.net/manual/function.setlocale
 */
setlocale(LC_ALL, 'en_US.utf-8');

/**
 * Enable the Kohana auto-loader.
 *
 * @link http://kohanaframework.org/guide/using.autoloading
 * @link http://www.php.net/manual/function.spl-autoload-register
 */
spl_autoload_register(array('Kohana', 'auto_load'));

/**
 * Optionally, you can enable a compatibility auto-loader for use with
 * older modules that have not been updated for PSR-0.
 *
 * It is recommended to not enable this unless absolutely necessary.
 */
//spl_autoload_register(array('Kohana', 'auto_load_lowercase'));

/**
 * Enable the Kohana auto-loader for unserialization.
 *
 * @link http://www.php.net/manual/function.spl-autoload-call
 * @link http://www.php.net/manual/var.configuration#unserialize-callback-func
 */
ini_set('unserialize_callback_func', 'spl_autoload_call');

/**
 * Set the mb_substitute_character to "none"
 *
 * @link http://www.php.net/manual/function.mb-substitute-character.php
 */
mb_substitute_character('none');

// -- Configuration and initialization -----------------------------------------

/**
 * Set the default language
 */
I18n::lang('ch'); //读取语言包

if (isset($_SERVER['SERVER_PROTOCOL']))
{
    // Replace the default protocol.
    HTTP::$protocol = $_SERVER['SERVER_PROTOCOL'];
}


//开发模式(Kohana::DEVELOPMENT) or 线上模式(Kohana::PRODUCTION),当项目上线时,应改为线上模式

Kohana::$environment = DEVELOPMENT_MODE ? Kohana::DEVELOPMENT : Kohana::PRODUCTION;

/**
 * Initialize Kohana, setting the default options.
 *
 * The following options are available:
 *
 * - string   base_url    path, and optionally domain, of your application   NULL
 * - string   index_file  name of your index file, usually "index.php"       index.php
 * - string   charset     internal character set used for input and output   utf-8
 * - string   cache_dir   set the internal cache directory                   APPPATH/cache
 * - integer  cache_life  lifetime, in seconds, of items cached              60
 * - boolean  errors      enable or disable error handling                   TRUE
 * - boolean  profile     enable or disable internal profiling               TRUE
 * - boolean  caching     enable or disable internal caching                 FALSE
 * - boolean  expose      set the X-Powered-By header                        FALSE
 */

//缓存目录,日志目录指定
$cache_dir = CACHE_DIR . 'v5';
$logs_dir = LOGS_DIR . 'v5';
if (!file_exists($cache_dir))
{
    if (!mkdir($cache_dir, 0755, true) && !is_dir($cache_dir)) {
        throw new \RuntimeException(sprintf('Directory "%s" was not created', $cache_dir));
    }
}
if (!file_exists($logs_dir))
{
    if (!mkdir($logs_dir, 0777, true) && !is_dir($logs_dir)) {
        throw new \RuntimeException(sprintf('Directory "%s" was not created', $logs_dir));
    }
}
Kohana::init(array(
    'base_url' => '/',
    'index_file' => '',
    'errors' => DEVELOPMENT_MODE,
    'cache_dir' => $cache_dir,
    'profile' => false,
    'caching' => true
));

/**
 * Attach the file write to logging. Multiple writers are supported.
 */
Kohana::$log->attach(new Log_File($logs_dir));

Kohana::$log_errors = true;//是否开启日志记录


/**
 * Attach a file reader to config. Multiple readers are supported.
 */
Kohana::$config->attach(new Config_File);

/**
 * Autoload composer libraries
 */
require DOCROOT . 'vendor/autoload.php';

/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
$default_modules = array(
    'developer' => BASEPATH.'/plugins/developer',
    'excaptha' => BASEPATH.'/plugins/excaptha',
    'cache' => MODPATH . 'cache',      // Caching with multiple backends
	'rediscache' => MODPATH . 'rediscache',
    'database' => MODPATH . 'database',   // Database access
    'image' => MODPATH . 'image',      // Image manipulation
    'orm' => MODPATH . 'orm',       // Object Relationship Mapping
    'pagination' => MODPATH . 'pagination', //分页
    'captcha' => MODPATH . 'captcha',//验证码类
    'cron' => MODPATH .'cron',//任务类
    'resque' => MODPATH.'resque',//异步队列
    'hooks' => MODPATH.'hooks',//事件类
    // 'auth'       => MODPATH.'auth',       // Basic authentication
    // 'minion'     => MODPATH.'minion',     // CLI Tasks
    //'codebench' => MODPATH . 'codebench',  // Benchmarking tool
    //'unittest'   => MODPATH.'unittest',   // Unit testing
    //'userguide' => MODPATH . 'userguide' // User guide and API documentation
);
$plugins_modules = include(BASEPATH . '/data/module.php');
foreach ($plugins_modules as $plugin_key => $plugin)
{
    $plugins_modules[$plugin_key] = BASEPATH . $plugin;
}
$default_modules = is_array($plugins_modules) ? array_merge($default_modules, $plugins_modules) : $default_modules;
//加载模块
Kohana::modules($default_modules);

/**
 * Set the routes. Each route must have a minimum of a name, a URI and a set of
 * defaults for the URI.
 */
require(APPPATH . 'route.php');
$cfg_basehost = St_Functions::get_http_prefix() . $_SERVER['HTTP_HOST']; //网站域名
$cfg_phone_cmspath = '';
$cfg_default_templet = '/default/';
$cfg_public_url = '/res/';
$cfg_res_url = '/res/';
$cfg_user_templet_dir = '/usertpl/';

//子站判断与处理
Common::cache_web_list();
//主站点webid
$sys_webid = 0;
//主站点prefix
$sys_prefix = Common::get_main_prefix();
//子站检测
if (file_exists(CACHE_DIR . 'v5/weblist.php'))
{
    /**
     * 站点列表
     * @var array $weblist
     */
    require_once CACHE_DIR . 'v5/weblist.php';
    $url = $_SERVER['HTTP_HOST'];//当前域名
    $uarr = explode('.', $url);
    $prefix = $uarr[0]; //当前域名前辍
    if (array_key_exists($prefix, $weblist))
    {
        //重置当前webid,prefix
        $sys_webid = $weblist[$prefix]['webid'];
        $sys_prefix = $prefix;
        $sys_destid = $sys_webid;
        $cfg_templet = 'substation';
    }
}
//写入/读取缓存
extract(Common::cache_config($sys_prefix, $sys_webid), EXTR_OVERWRITE);
Cookie::$httponly = TRUE;
Cookie::$salt = 'stourwebcms';
Cookie::$path = '/';
Cookie::$domain = Common::cookie_domain();
//加载事件配置文件
Hook::load();
//任务初始化
St_Task::init();
/**
 * saas小程序订单监听,回调处理
 */
$saas_xcx = include(BASEPATH . '/data/saas_xcx.php');
//0：关闭pc站点
if(isset($saas_xcx['pc']) && $saas_xcx['pc'] == 0)
{
    // 跳过订单监听内地址
    $listener = DB::select("execute_url")->from("member_order_listener")->where("isenabled","=","1")->execute()->as_array();
    $xcx_go = false;
    $pass_item = array(
        array(
            'execute_url'=> 'api/v2' // uniapp程序接口
        ),
        array(
            'execute_url'=> 'api/standard' // 小程序接口
        ),
        array(
            'execute_url'=> 'connect/' // 智旅互联
        )
        ,
        array(
            'execute_url'=> 'uploads/' // 图片
        ),
        array(
            'execute_url'=> 'myxcx'
        ),
        array(
            'execute_url'=> 'mobile'
        ),
        array(
            'execute_url'=> 'phone'
        ),
        array(
            'execute_url'=> 'toutiao'
        )
        ,
        array(
            'execute_url'=> 'api/service'
        ),
        array(
            'execute_url'=> 'supplier_brokerage'
        ),
        array(
            'execute_url'=> 'approval'
        )
    );
    $listener = array_merge($listener,$pass_item);
    foreach($listener as $ev)
    {
        if(strpos($_SERVER['REQUEST_URI'],$ev['execute_url'])){
            $xcx_go = true;
        }
    }
    if(!$xcx_go)
    {
        $content=Model_Sysconfig::get_configs(0,'pc_close_content',true);
        exit('<meta charset="utf-8"><p align="center">'.$content.'</p>');
    }
}


//手机跳转,判断是否是手机浏览
if ($cfg_mobile_open == '1' )
{

    if (Common::is_mobile() && !preg_match('/^[\/\\\]api[\/\\\]/i', $_SERVER['REQUEST_URI']) && !preg_match('/^[\/\\\]myxcx[\/\\\]/i', $_SERVER['REQUEST_URI']))
    {
        //兼容性修复
        $uri = $_SERVER["HTTP_X_REWRITE_URL"];
        if ($uri == null)
        {
            $uri = $_SERVER["REQUEST_URI"];
        }
        //检测域名
        if (!preg_match('~/?uploads/~', $uri))
        {
            $mobile = include SLINEDATA . '/mobile.php';

            // 主域名默认进入的端
            $mainUrl = str_replace(array('http://','https://'),'', $mobile['domain']['main'] );
            if(strlen($_SERVER['REQUEST_URI']) == 1 && $_SERVER['HTTP_HOST'] == $mainUrl){
                if($cfg_m_default_client == 1){
                    $url = $mobile['domain']['main'].'/h5';
                    header("Location:$url");//跳转到手机页面
                    exit();
                }
            }
			// 跳老版phone
            $path = rtrim($mobile['version'][$cfg_mobile_version]['path'], '/');
            $url = St_Functions::get_http_prefix() . $_SERVER['HTTP_HOST'] . $path . $uri;
            if (substr_count($mobile['domain']['mobile'], $_SERVER['HTTP_HOST']) && !substr_count($mobile['domain']['main'], $_SERVER['HTTP_HOST']))
            {
                $url = $mobile['domain']['mobile'] . $uri;
            }

            //手机版子站内容匹配
            $c_host_arr=explode('.',$_SERVER['HTTP_HOST']);
            $m_host=str_replace(St_Functions::get_http_prefix(),'',$mobile['domain']['mobile']);
            $m_host_arr=explode('.',$m_host);
            if($m_host_arr[0]!==$c_host_arr[0]&&!substr_count($mobile['domain']['main'], $_SERVER['HTTP_HOST']))
            {
                $url=$mobile['domain']['main'].$path.'/'.$c_host_arr[0].$uri;
            }
            header("Location:$url");//跳转到手机页面
            exit();
        }
    }
}